/****************************************************************
 *Author:       zxx
 *UnityVersion：2019.4.1f1
 *Date:         2020/07/31 14:37:05
 *Description:    
*****************************************************************/
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using DG.Tweening;

public class RocketSpawner : MonoBehaviour {

    private Vector3 targetPoint;

    private Vector3 spawnPoint;

    public float speed =10;

    public float leftOffsetX = 100;

    private void Start()
    {
        spawnPoint = transform.position;

        targetPoint = new Vector3(spawnPoint.x - leftOffsetX, spawnPoint.y, spawnPoint.z);
     
        InvokeRepeating("SpwanRocket", 1, 1);
    }

    private void Awake()
    {
        EventCenter.AddListener(EventDefine.ReocketSpawn, SpwanRocket);
    }

    private void OnDestroy()
    {
        EventCenter.RemoveListener(EventDefine.ReocketSpawn, SpwanRocket);

    }


    private void SpwanRocket()
    {
        if (Random.Range(0, 100) >90)
        {


            GameObject go = ObjectPool.Instance.GetRocket();
            if (targetPoint != null && spawnPoint != null)
            {
                go.transform.position = spawnPoint;

                float time = Vector3.Distance(targetPoint, spawnPoint) / speed;
                go.SetActive(true);
                go.transform.DOMove(targetPoint, time).OnComplete(() =>
                {

                    go.SetActive(false);
                });

            }
        }

    }
}

